﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SchemaMonitor;
using Framework;

public enum ESortBy
{
    Nearest = 0,
    LeastTags = 1,
    Random = 2
}

/// <summary>
/// Summary description for CSession
/// </summary>
public class CSession : CSessionBase
{
    public static CUser User
    {
        get
        {
            var u = (CUser)CApplication.Get("APP.User");
            if (null == u)
            {
                u = new CUser(1);
                CApplication.Set("APP.User", u);
            }
            return u;
        }
    }

    public static string SearchChapters { get { return GetStr("SearchChapters"); } set { Set("SearchChapters", value); } }
    public static string SearchData { get { return GetStr("SearchData"); } set { Set("SearchData", value); } }
    public static bool SearchArchived { get { return GetBool("SearchArchived"); } set { SetBool("SearchArchived", value); } }
    public static int ActiveViewIndex { get { return GetInt("ActiveViewIndex", 0); } set { SetInt("ActiveViewIndex", value); } }
    public static int TypeId { get { return GetInt("typeId"); } set { SetInt("typeId", value); } }
    public static int TagTypeId { get { return GetInt("tagTypeId"); } set { SetInt("tagTypeId", value); } }
    public static int TagId { get { return GetInt("TagId"); } set { SetInt("TagId", value); } }

    public static ESortBy SortBy_         { get { return (ESortBy)SortBy;     } set { SortBy = (int)value;     } }
    public static int  SortBy             { get { return GetInt("SortBy", 0); } set { SetInt("SortBy", value); } }
    public static int  FilterByHasTagId   { get { return GetInt("FilterByHasTagId");    } set { SetInt("FilterByHasTagId",  value); } }
    public static int  FilterByNotTagId   { get { return GetInt("FilterByNotTagId");    } set { SetInt("FilterByNotTagId",  value); } }
    public static bool FilterByNoTag      { get { return GetBool("FilterByNoTag", true);} set { SetBool("FilterByNoTag",    value); } }
    public static bool FilterByNoPost     { get { return GetBool("FilterByNoPost");     } set { SetBool("FilterByNoPost",   value); } }
    public static bool FilterByHasCaption { get { return GetBool("FilterByCaption");    } set { SetBool("FilterByCaption",  value); } }
    public static bool ShowingXRated      { get { return GetBool("ShowingXRated");      } set { SetBool("ShowingXRated",    value); } }
    public static bool ShowingDeleted     { get { return GetBool("ShowingDeleted");     } set { SetBool("ShowingDeleted",   value); } }

    public static bool Random { get { return SortBy_ == ESortBy.Random; } }
    public static bool ByDate { get { return SortBy_ == ESortBy.Nearest; } }

    public static int ChapterId { get { return GetInt("ChapterId"); } set { SetInt("ChapterId", value); } }
    public static int BookId { get { return GetInt("bookId"); } set { SetInt("bookId", value); } }
    public static int SectionId { get { return GetInt("sectionId"); } set { SetInt("sectionId", value); } }
    public static bool ExcludeAll { get { return GetBool("ExcludeAll", true); } set { SetBool("ExcludeAll", value); } }

    public static string PageMessage { get { return GetStr("PageMessage"); } set { SetStr("PageMessage", value); } }

    public static CPhotoHashList Shuffled(CPhotoHashList list, bool force = false)
    {
        var key = "Shuffled";
        var obj = (CPhotoHashList)GetObj(key);
        if (null == obj || force == true)
        {
            if (null == list)
                SetObj(key, null);
            else
            {
                obj = new CPhotoHashList(CShuffle.Shuffle(list));
                SetObj(key, obj);
            }
        }
        return obj;
    }
	public static void Shuffle() { Shuffled(null, true); }
}